/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.AIDao;
import org.gov.vis.data.dao.AIDaoImpl;
import org.gov.vis.data.dto.AIDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author Vishwanath
 *
 */
public class AICtlrImpl implements AICtlr {

	/**
	 * Method called to Add or Edit the AI
	 * @param aiDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditAI(final AIDto _aiDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _aiCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_ai_code"), _aiDto.getCode(), 1, 20, true);
			if(_aiCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_code").toUpperCase(), _aiCodeErr);

			String _aiBullNoErr = ValidatorUtils.validateString(rb.getString("dialog_label_bullno"), _aiDto.getBullNo(), 1, 50, true);
			if(_aiBullNoErr != null)
				errorMsgs.put(rb.getString("dialog_label_bullno").toUpperCase(), _aiBullNoErr);
			
			String _aiDOIErr = ValidatorUtils.validateString(rb.getString("dialog_label_ai_dtofinsumnaton"), DU.format(_aiDto.getDoi(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_aiDOIErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_dtofinsumnaton").toUpperCase(), _aiDOIErr);
			
			String _aiBreedErr = ValidatorUtils.validateString(rb.getString("dialog_label_ai_breed"), _aiDto.getBreed(), 3, 50, true);
			if(_aiBreedErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_breed").toUpperCase(), _aiBreedErr);
			
		//	String _aiAnimalTypeErr = ValidatorUtils.validateDropDown(rb.getString("dialog_label_ai_animal_type"), Long.parseLong(_aiDto.getAnimalType()), 1);
		//	if(_aiAnimalTypeErr != null)
			//	errorMsgs.put(rb.getString("dialog_label_ai_animal_type").toUpperCase(), _aiAnimalTypeErr);
			
			String _aiKindOfAnimalErr = ValidatorUtils.validateString(rb.getString("dialog_label_ai_kndofanml"), _aiDto.getKindOfAnimal(), 3, 50, true);
			if(_aiKindOfAnimalErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_kndofanml").toUpperCase(), _aiKindOfAnimalErr);
			
			
			//Births Given is NOT a required field but, here, we are checking on numeric validation
			String _aiBirthsGivenErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_ai_brthgvn"), _aiDto.getBirthGiven(), 0, 50, false, false);
			if(_aiBirthsGivenErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_brthgvn").toUpperCase(), _aiBirthsGivenErr);
			
			String _aiNoOfAIErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_ai_noai"), _aiDto.getNoOfAI(), 0, 50, false, false);
			if(_aiNoOfAIErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_noai").toUpperCase(), _aiNoOfAIErr);
			
			String _aiFeeErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_ai_aifee"), _aiDto.getFee(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_aiFeeErr != null)
				errorMsgs.put(rb.getString("dialog_label_ai_aifee").toUpperCase(), _aiFeeErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists anai AI Object with the same Code
			//If Code has been duplicated, then throw the alert
			AIDto aiDto = null;
			if(_aiDto.getAiId() > 0){
				//get the AIDto on the passed Code name
				aiDto = this.getAIOnCode(_aiDto.getCode());
				if(aiDto != null && aiDto.getAiId()!= _aiDto.getAiId()){
					errorMsgs.put(rb.getString("dialog_label_ai_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_AI_MSG));
				}
			}//end if
			else{
				//get the AIDto on the passed Code name
				aiDto = this.getAIOnCode(_aiDto.getCode());
				if(aiDto != null){
					errorMsgs.put(rb.getString("dialog_label_ai_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_AI_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final AIDao _aiDao = new AIDaoImpl();

			//add or edit the AI Object
			_aiDao.addOrEditAI(_aiDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_aiDto.getAiId() > 0){
				throw new SystemException(rb.getString(GuiConstants.DATA_EDITAI_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.DATA_ADDAI_EXCEPTION));
			}
		}
	}//end addOrEditAI

	/**
	 * Method called to delete the AI Object based on the passed ai Id
	 * @param _aiId
	 * @throws Exception
	 */
	@Override
	public void deleteAI(final long _aiId) throws Exception {
		try{
			final AIDao _aiDao = new AIDaoImpl();
			_aiDao.deleteAI(_aiId);
		}
		catch (Exception ex) {
			ex.printStackTrace();
			throw new SystemException(rb.getString(GuiConstants.DATA_DELETEAI_EXCEPTION));
		}
	}//end deleteAI

	/**
	 * Method called to get the AI based on the passed primary key Id
	 * @param _aiId
	 * @return
	 * @throws Exception
	 */
	@Override
	public AIDto getAI(final long _aiId) throws Exception {
		try{
			final AIDao _aiDao = new AIDaoImpl();
			AIDto _aiDto = new AIDto();
			_aiDto.setAiId(_aiId);

			AIDto aiDto = null;
			List<AIDto> _aiList = _aiDao.getAIs(_aiDto);
			if(_aiList != null && !_aiList.isEmpty()){
				aiDto = _aiList.get(0);
			}

			return aiDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_EDITAI_EXCEPTION));
		}
	}//end getAI

	/**
	 * Method called to get the AI based on the passed Code
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public AIDto getAIOnCode(final String _code) throws Exception {
		final AIDao _aiDao = new AIDaoImpl();
		AIDto _aiDto = new AIDto();
		_aiDto.setCode(_code);

		AIDto aiDto = null;
		List<AIDto> _aiList = _aiDao.getAIs(_aiDto);
		if(_aiList != null && !_aiList.isEmpty()){
			aiDto = _aiList.get(0);
		}

		return aiDto;
	}//end getAIOnCode

	/**
	 * Method called to get the AI based on the passed Breed
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public AIDto getAIOnBreed(final String _breed) throws Exception {
		final AIDao _aiDao = new AIDaoImpl();
		AIDto _aiDto = new AIDto();
		_aiDto.setBreed(_breed);

		AIDto aiDto = null;
		List<AIDto> _aiList = _aiDao.getAIs(_aiDto);
		if(_aiList != null && !_aiList.isEmpty()){
			aiDto = _aiList.get(0);
		}

		return aiDto;
	}//end getAIOnBreed

	/**
	 * Method called to get the AI based on the passed BullNo
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public AIDto getAIOnBullNo(final String _bullNo) throws Exception {
		final AIDao _aiDao = new AIDaoImpl();
		AIDto _aiDto = new AIDto();
		_aiDto.setBullNo(_bullNo);

		AIDto aiDto = null;
		List<AIDto> _aiList = _aiDao.getAIs(_aiDto);
		if(_aiList != null && !_aiList.isEmpty()){
			aiDto = _aiList.get(0);
		}

		return aiDto;
	}//end getAIOnBullNo

	/**
	 * Method called to get the AI based on the passed AI Object
	 * @param aiDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public AIDto getAI(final AIDto _aiDto) throws Exception {
		final AIDao _aiDao = new AIDaoImpl();

		AIDto aiDto = null;
		List<AIDto> _aiList = _aiDao.getAIs(_aiDto);
		if(_aiList != null && !_aiList.isEmpty()){
			aiDto = _aiList.get(0);
		}

		return aiDto;
	}//end getAIs

	/**
	 * Method called to get the list of AI based on the passed AI Object
	 * @param aiDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<AIDto> getAIs(final AIDto _aiDto) throws Exception {
		final AIDao _aiDao = new AIDaoImpl();
		return _aiDao.getAIs(_aiDto);
	}//end getAIs

	/**
	 * Method called to search for the AIs based on the passed AI Object
	 * @param aiDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<AIDto> searchAIs(AIDto _aiDto) throws Exception {
		try{
			final AIDao _aiDao = new AIDaoImpl();
			return _aiDao.searchAIs(_aiDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_MAINTAINAI_EXCEPTION));
		}
	}//end searchAIs

}//end class
